#include <string_view>
#include <string>
#include <vector>
#include <iostream>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <forward_list>
#include <unordered_map>
#include <set>
#include <unordered_set>
// #include <priority_queue>
#include <stack>
// #include <unordered_multimap>
#include <array>
#include <algorithm>

using namespace std;

#include <assert.h>
#include <string.h>
#include <string>

#include <iostream>

 void mymemcpy(void* psrc, void* pdst, size_t length) {
     assert(psrc && pdst);
     char* p1 = (char*) psrc;
     char* p2 = (char*) pdst;
     if (p1 + length > p2 && p1 < p2) {
         char* p2End = p2 + length-1;
         char* p1End = p1 + length-1;
         while (length--) {
             *p2End-- = *p1End--;
         }
     } else {
         while (length--) {
             *p2++ = *p1++;
         }
     }
 }

void reverseWord(std::string& str, int start, int end) {
    while (start < end) {
        char tmp = str[start];
        str[start] = str[end];
        str[end] = tmp;
        //std::swap(str[start], str[end]);
        start++;
        end--;
    }
}

void reverseSubSentence(std::string& str, int start, int end) {
    int i;
    int left = start;
    int right = 0;

    for (i = start; i <= end; i++) {
        if (str[i] == ' ') {
            right = i-1;
            reverseWord(str, left, right);
            left = i+1;
        } else if (i == end) {
            right = i-1;
            reverseWord(str, left, end);
            left = i+1;
        }
    }
    reverseWord(str, start, end);
}

void reverseSentence(std::string& str, int start, int end) {
    int i;
    int left = start;
    int right = 0;
    for (i = start; i <= end; i++) {
        if (str[i] == ',') {
            right = i-1;
            reverseSubSentence(str, left, right);
            left = i+1;
        } else if (i == end) {
            reverseSubSentence(str, left, end);
        }
    }

}

int main() {
    std::string str = ",hello world,god bless you,,,,hello world";
    std::string subStr = "god bless you";
    reverseSentence(str, 0, str.size()-1);
    std::cout << str;
}